#include "view_driver.h"

#include <stdint.h>

#include "cmsis_os2.h"
#include "control_driver.h"
#include "usart.h"
//
// Created by 86156 on 25-8-27.
//

int16_t X_Measure=0;
int16_t Y_Measure=0;
int16_t Dis_Measure=0;

uint16_t Crc_TX = 0;
uint16_t Crc_RX = 0;

uint8_t View_RxBuffer[View_RxBufferSize];
uint8_t View_TxBuffer[View_TxBufferSize];

uint8_t View_Connected = 0;

uint8_t ProcessFlag;

uint8_t left_right_flag=0;
uint8_t up_down_flag=0;
uint8_t forward_back_flag=0;
extern osSemaphoreId_t ViewSemBinaryHandle;


void View_SendData() {
    View_TxBuffer[0] = 0x55;
    View_TxBuffer[1] = ProcessFlag;
    View_TxBuffer[2] = (uint8_t)((int16_t)Angle_Measure.yaw >> 8);
    View_TxBuffer[3] = (uint8_t)((int16_t)Angle_Measure.yaw&0xFF);
    View_TxBuffer[4] = '\n';
    HAL_UART_Transmit_IT(&huart1, View_TxBuffer, View_TxBufferSize);
}



void View_Init(){
    HAL_UARTEx_ReceiveToIdle_IT(&huart1, View_RxBuffer, View_RxBufferSize);
    View_SendData();
    //__HAL_DMA_DISABLE_IT(huart6.hdmarx, DMA_IT_HT);
}

void View_Data_Wrong();






void View_Decode(uint8_t *buf, uint16_t len) {
    if (buf[0] == 0x99) {
        __set_FAULTMASK(1);//禁止所有的可屏蔽中断
        NVIC_SystemReset();//软件复位
    }
    if (buf[0] != 0x55) {
        View_Connected = 2;
        return;
    }
    ProcessFlag = buf[1];
    left_right_flag = buf[2];
    up_down_flag = buf[3];
    forward_back_flag = buf[4];
    // X_Measure = (int16_t)((int16_t) buf[4] |  (int16_t) buf[5] << 8);
    // Y_Measure = (int16_t)((int16_t) buf[6] |  (int16_t) buf[7] << 8);

}




// void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) {
//     if(huart->Instance == huart1.Instance)
//     {
//         osSemaphoreRelease(ViewSemBinaryHandle);
//     }
//
// }
// void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
//
// }
